﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using LibKetoan;
using LibDuoc;

namespace Ketoan
{
	/// <summary>
	/// Summary description for frmImpvienphi.
	/// </summary>
	public class frmImpvienphi : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.DateTimePicker tu;
		private System.Windows.Forms.DateTimePicker den;
		private System.Windows.Forms.TextBox sophieu;
		private System.Windows.Forms.TextBox madv;
		private System.Windows.Forms.DataGrid dataGrid1;
		private System.Windows.Forms.Button butLuu;
		private System.Windows.Forms.Button butKetthuc;
		private System.Windows.Forms.Button butTonghop;
		private int i_userd;
		private string s_mmyy,sql,s_ngay,user,sNoidung,tkno="1111";
		private LibKetoan.AccessData a;
        private LibDuoc.AccessData d = new LibDuoc.AccessData();
		private DataSet ds;
		private DataSet dsxml=new DataSet();
		private DataTable dtloai=new DataTable();
		private DataTable dttk=new DataTable();
		private DataTable dtdt=new DataTable();
		private Brush disabledBackBrush;
		private Brush disabledTextBrush;
		private Brush alertBackBrush;
		private Font alertFont;
		private Brush alertTextBrush;
		private Font currentRowFont;
		private Brush currentRowBackBrush;
		private bool afterCurrentCellChanged,bClear=true;
		private int checkCol=0,iHoatdong,iLctug,iChuong,iNghiepvu,iLoaibk,iLk,iCapphat,iChucnang=1,iUserid,iTonghop=1,iNhom,iKhoanmucno,iTcnkp;
		private System.Windows.Forms.CheckBox chkAll;
		private System.Windows.Forms.ToolTip toolTip1;
		private System.Windows.Forms.ComboBox c710;
		private System.Windows.Forms.Label label42;
		private System.Windows.Forms.ComboBox c707;
		private System.Windows.Forms.Label label39;
		private System.Windows.Forms.ComboBox c706;
		private System.Windows.Forms.Label label50;
		private System.Windows.Forms.ComboBox c705;
		private System.Windows.Forms.Label label38;
		private System.Windows.Forms.ComboBox c703;
		private System.Windows.Forms.ComboBox c702;
		private System.Windows.Forms.Label label36;
		private System.Windows.Forms.Label label37;
		private System.Windows.Forms.ComboBox c700;
		private System.Windows.Forms.Label label35;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.ComboBox khoanmuc;
		private System.Windows.Forms.ComboBox khoanmuc2;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.ComboBox khoanmuc4;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.ComboBox khoanmuc3;
		private System.Windows.Forms.Label label10;
		private System.Windows.Forms.Label label68;
		private System.Windows.Forms.ComboBox c904;
		private System.ComponentModel.IContainer components;

		public frmImpvienphi(LibKetoan.AccessData acc,string mmyy,string ngay,int userid)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			a=acc;s_mmyy=mmyy;i_userd=userid;s_ngay=ngay;
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
					if(disabledBackBrush != null)
					{
						disabledBackBrush.Dispose();
						disabledTextBrush.Dispose();
						alertBackBrush.Dispose();
						alertFont.Dispose();
						alertTextBrush.Dispose();
						currentRowFont.Dispose();
						currentRowBackBrush.Dispose();
					}
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmImpvienphi));
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.tu = new System.Windows.Forms.DateTimePicker();
            this.den = new System.Windows.Forms.DateTimePicker();
            this.sophieu = new System.Windows.Forms.TextBox();
            this.madv = new System.Windows.Forms.TextBox();
            this.dataGrid1 = new System.Windows.Forms.DataGrid();
            this.butLuu = new System.Windows.Forms.Button();
            this.butKetthuc = new System.Windows.Forms.Button();
            this.butTonghop = new System.Windows.Forms.Button();
            this.chkAll = new System.Windows.Forms.CheckBox();
            this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
            this.c710 = new System.Windows.Forms.ComboBox();
            this.label42 = new System.Windows.Forms.Label();
            this.c707 = new System.Windows.Forms.ComboBox();
            this.label39 = new System.Windows.Forms.Label();
            this.c706 = new System.Windows.Forms.ComboBox();
            this.label50 = new System.Windows.Forms.Label();
            this.c705 = new System.Windows.Forms.ComboBox();
            this.label38 = new System.Windows.Forms.Label();
            this.c703 = new System.Windows.Forms.ComboBox();
            this.c702 = new System.Windows.Forms.ComboBox();
            this.label36 = new System.Windows.Forms.Label();
            this.label37 = new System.Windows.Forms.Label();
            this.c700 = new System.Windows.Forms.ComboBox();
            this.label35 = new System.Windows.Forms.Label();
            this.label7 = new System.Windows.Forms.Label();
            this.khoanmuc = new System.Windows.Forms.ComboBox();
            this.khoanmuc2 = new System.Windows.Forms.ComboBox();
            this.label8 = new System.Windows.Forms.Label();
            this.khoanmuc4 = new System.Windows.Forms.ComboBox();
            this.label9 = new System.Windows.Forms.Label();
            this.khoanmuc3 = new System.Windows.Forms.ComboBox();
            this.label10 = new System.Windows.Forms.Label();
            this.label68 = new System.Windows.Forms.Label();
            this.c904 = new System.Windows.Forms.ComboBox();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(224, 50);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(56, 23);
            this.label1.TabIndex = 24;
            this.label1.Text = "Tử ngày :";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(354, 50);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(16, 23);
            this.label2.TabIndex = 26;
            this.label2.Text = "-";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(432, 50);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(72, 23);
            this.label3.TabIndex = 28;
            this.label3.Text = "Loại thu :";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label5
            // 
            this.label5.Location = new System.Drawing.Point(600, 50);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(80, 23);
            this.label5.TabIndex = 32;
            this.label5.Text = "Thu ngân :";
            this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // tu
            // 
            this.tu.CustomFormat = "dd/MM/yyyy";
            this.tu.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tu.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
            this.tu.Location = new System.Drawing.Point(280, 51);
            this.tu.Name = "tu";
            this.tu.Size = new System.Drawing.Size(80, 21);
            this.tu.TabIndex = 12;
            this.tu.Value = new System.DateTime(2005, 5, 26, 11, 8, 30, 30);
            this.tu.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // den
            // 
            this.den.CustomFormat = "dd/MM/yyyy";
            this.den.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.den.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
            this.den.Location = new System.Drawing.Point(368, 51);
            this.den.Name = "den";
            this.den.Size = new System.Drawing.Size(80, 21);
            this.den.TabIndex = 13;
            this.den.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // sophieu
            // 
            this.sophieu.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.sophieu.ForeColor = System.Drawing.Color.Red;
            this.sophieu.Location = new System.Drawing.Point(504, 51);
            this.sophieu.Name = "sophieu";
            this.sophieu.Size = new System.Drawing.Size(120, 21);
            this.sophieu.TabIndex = 229;
            this.sophieu.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.sophieu.TextChanged += new System.EventHandler(this.sophieu_TextChanged);
            this.sophieu.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // madv
            // 
            this.madv.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.madv.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.madv.ForeColor = System.Drawing.Color.Red;
            this.madv.Location = new System.Drawing.Point(680, 51);
            this.madv.Name = "madv";
            this.madv.Size = new System.Drawing.Size(104, 21);
            this.madv.TabIndex = 213;
            this.madv.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.madv.TextChanged += new System.EventHandler(this.madv_TextChanged);
            this.madv.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // dataGrid1
            // 
            this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.dataGrid1.BackgroundColor = System.Drawing.SystemColors.Control;
            this.dataGrid1.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.dataGrid1.CaptionBackColor = System.Drawing.SystemColors.Control;
            this.dataGrid1.DataMember = "";
            this.dataGrid1.FlatMode = true;
            this.dataGrid1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
            this.dataGrid1.Location = new System.Drawing.Point(8, 58);
            this.dataGrid1.Name = "dataGrid1";
            this.dataGrid1.RowHeaderWidth = 5;
            this.dataGrid1.Size = new System.Drawing.Size(776, 412);
            this.dataGrid1.TabIndex = 124;
            this.dataGrid1.CurrentCellChanged += new System.EventHandler(this.dataGrid1_CurrentCellChanged);
            this.dataGrid1.Click += new System.EventHandler(this.dataGrid1_Click);
            // 
            // butLuu
            // 
            this.butLuu.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.butLuu.Image = ((System.Drawing.Image)(resources.GetObject("butLuu.Image")));
            this.butLuu.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butLuu.Location = new System.Drawing.Point(361, 476);
            this.butLuu.Name = "butLuu";
            this.butLuu.Size = new System.Drawing.Size(74, 25);
            this.butLuu.TabIndex = 15;
            this.butLuu.Text = "&Cập nhật";
            this.butLuu.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butLuu.Click += new System.EventHandler(this.butLuu_Click);
            // 
            // butKetthuc
            // 
            this.butKetthuc.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.butKetthuc.Image = ((System.Drawing.Image)(resources.GetObject("butKetthuc.Image")));
            this.butKetthuc.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butKetthuc.Location = new System.Drawing.Point(438, 476);
            this.butKetthuc.Name = "butKetthuc";
            this.butKetthuc.Size = new System.Drawing.Size(70, 25);
            this.butKetthuc.TabIndex = 16;
            this.butKetthuc.Text = "   &Kết thúc";
            this.butKetthuc.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butKetthuc.Click += new System.EventHandler(this.butKetthuc_Click);
            // 
            // butTonghop
            // 
            this.butTonghop.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.butTonghop.Image = ((System.Drawing.Image)(resources.GetObject("butTonghop.Image")));
            this.butTonghop.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butTonghop.Location = new System.Drawing.Point(284, 476);
            this.butTonghop.Name = "butTonghop";
            this.butTonghop.Size = new System.Drawing.Size(74, 25);
            this.butTonghop.TabIndex = 14;
            this.butTonghop.Text = "&Tổng hợp";
            this.butTonghop.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butTonghop.Click += new System.EventHandler(this.butTonghop_Click);
            // 
            // chkAll
            // 
            this.chkAll.Appearance = System.Windows.Forms.Appearance.Button;
            this.chkAll.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.chkAll.Location = new System.Drawing.Point(25, 99);
            this.chkAll.Name = "chkAll";
            this.chkAll.Size = new System.Drawing.Size(23, 21);
            this.chkAll.TabIndex = 129;
            this.chkAll.Text = "...";
            this.chkAll.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.toolTip1.SetToolTip(this.chkAll, "Chọn cả");
            this.chkAll.CheckedChanged += new System.EventHandler(this.chkAll_CheckedChanged);
            // 
            // c710
            // 
            this.c710.BackColor = System.Drawing.SystemColors.HighlightText;
            this.c710.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.c710.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.c710.Location = new System.Drawing.Point(48, 5);
            this.c710.Name = "c710";
            this.c710.Size = new System.Drawing.Size(168, 21);
            this.c710.TabIndex = 0;
            this.c710.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // label42
            // 
            this.label42.Location = new System.Drawing.Point(-16, 3);
            this.label42.Name = "label42";
            this.label42.Size = new System.Drawing.Size(64, 23);
            this.label42.TabIndex = 0;
            this.label42.Text = "Nhóm :";
            this.label42.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // c707
            // 
            this.c707.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.c707.BackColor = System.Drawing.SystemColors.HighlightText;
            this.c707.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.c707.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.c707.Location = new System.Drawing.Point(680, 28);
            this.c707.Name = "c707";
            this.c707.Size = new System.Drawing.Size(104, 21);
            this.c707.TabIndex = 10;
            this.c707.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // label39
            // 
            this.label39.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.label39.Location = new System.Drawing.Point(600, 27);
            this.label39.Name = "label39";
            this.label39.Size = new System.Drawing.Size(80, 23);
            this.label39.TabIndex = 20;
            this.label39.Text = "Cấp phát :";
            this.label39.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // c706
            // 
            this.c706.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.c706.BackColor = System.Drawing.SystemColors.HighlightText;
            this.c706.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.c706.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.c706.Location = new System.Drawing.Point(280, 28);
            this.c706.Name = "c706";
            this.c706.Size = new System.Drawing.Size(168, 21);
            this.c706.TabIndex = 8;
            this.c706.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // label50
            // 
            this.label50.Location = new System.Drawing.Point(208, 27);
            this.label50.Name = "label50";
            this.label50.Size = new System.Drawing.Size(72, 23);
            this.label50.TabIndex = 16;
            this.label50.Text = "Loại khoản :";
            this.label50.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // c705
            // 
            this.c705.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.c705.BackColor = System.Drawing.SystemColors.HighlightText;
            this.c705.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.c705.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.c705.Location = new System.Drawing.Point(504, 28);
            this.c705.Name = "c705";
            this.c705.Size = new System.Drawing.Size(120, 21);
            this.c705.TabIndex = 9;
            this.c705.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // label38
            // 
            this.label38.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.label38.Location = new System.Drawing.Point(424, 27);
            this.label38.Name = "label38";
            this.label38.Size = new System.Drawing.Size(80, 23);
            this.label38.TabIndex = 18;
            this.label38.Text = "Loại b.kê :";
            this.label38.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // c703
            // 
            this.c703.BackColor = System.Drawing.SystemColors.HighlightText;
            this.c703.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.c703.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.c703.Location = new System.Drawing.Point(48, 28);
            this.c703.Name = "c703";
            this.c703.Size = new System.Drawing.Size(168, 21);
            this.c703.TabIndex = 7;
            this.c703.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // c702
            // 
            this.c702.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.c702.BackColor = System.Drawing.SystemColors.HighlightText;
            this.c702.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.c702.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.c702.Location = new System.Drawing.Point(680, 5);
            this.c702.Name = "c702";
            this.c702.Size = new System.Drawing.Size(104, 21);
            this.c702.TabIndex = 2;
            this.c702.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // label36
            // 
            this.label36.Location = new System.Drawing.Point(-16, 27);
            this.label36.Name = "label36";
            this.label36.Size = new System.Drawing.Size(64, 23);
            this.label36.TabIndex = 14;
            this.label36.Text = "N. vụ :";
            this.label36.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label37
            // 
            this.label37.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.label37.Location = new System.Drawing.Point(624, 3);
            this.label37.Name = "label37";
            this.label37.Size = new System.Drawing.Size(56, 23);
            this.label37.TabIndex = 4;
            this.label37.Text = "Chương :";
            this.label37.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // c700
            // 
            this.c700.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.c700.BackColor = System.Drawing.SystemColors.HighlightText;
            this.c700.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.c700.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.c700.Location = new System.Drawing.Point(280, 5);
            this.c700.Name = "c700";
            this.c700.Size = new System.Drawing.Size(344, 21);
            this.c700.TabIndex = 1;
            this.c700.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // label35
            // 
            this.label35.Location = new System.Drawing.Point(216, 3);
            this.label35.Name = "label35";
            this.label35.Size = new System.Drawing.Size(64, 23);
            this.label35.TabIndex = 2;
            this.label35.Text = "Hoạt động :";
            this.label35.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label7
            // 
            this.label7.Enabled = false;
            this.label7.Location = new System.Drawing.Point(774, 557);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(10, 23);
            this.label7.TabIndex = 6;
            this.label7.Text = "K. mục 1 :";
            this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.label7.Visible = false;
            // 
            // khoanmuc
            // 
            this.khoanmuc.BackColor = System.Drawing.SystemColors.HighlightText;
            this.khoanmuc.Enabled = false;
            this.khoanmuc.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.khoanmuc.Location = new System.Drawing.Point(770, 557);
            this.khoanmuc.Name = "khoanmuc";
            this.khoanmuc.Size = new System.Drawing.Size(10, 21);
            this.khoanmuc.TabIndex = 3;
            this.khoanmuc.Visible = false;
            this.khoanmuc.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // khoanmuc2
            // 
            this.khoanmuc2.BackColor = System.Drawing.SystemColors.HighlightText;
            this.khoanmuc2.Enabled = false;
            this.khoanmuc2.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.khoanmuc2.Location = new System.Drawing.Point(770, 559);
            this.khoanmuc2.Name = "khoanmuc2";
            this.khoanmuc2.Size = new System.Drawing.Size(10, 21);
            this.khoanmuc2.TabIndex = 4;
            this.khoanmuc2.Visible = false;
            this.khoanmuc2.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // label8
            // 
            this.label8.Enabled = false;
            this.label8.Location = new System.Drawing.Point(774, 557);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(10, 23);
            this.label8.TabIndex = 8;
            this.label8.Text = "K. mục 2 :";
            this.label8.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.label8.Visible = false;
            // 
            // khoanmuc4
            // 
            this.khoanmuc4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.khoanmuc4.BackColor = System.Drawing.SystemColors.HighlightText;
            this.khoanmuc4.Enabled = false;
            this.khoanmuc4.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.khoanmuc4.Location = new System.Drawing.Point(770, 557);
            this.khoanmuc4.Name = "khoanmuc4";
            this.khoanmuc4.Size = new System.Drawing.Size(10, 21);
            this.khoanmuc4.TabIndex = 6;
            this.khoanmuc4.Visible = false;
            this.khoanmuc4.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // label9
            // 
            this.label9.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.label9.Enabled = false;
            this.label9.Location = new System.Drawing.Point(774, 557);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(10, 23);
            this.label9.TabIndex = 12;
            this.label9.Text = "K. mục 4 :";
            this.label9.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.label9.Visible = false;
            // 
            // khoanmuc3
            // 
            this.khoanmuc3.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.khoanmuc3.BackColor = System.Drawing.SystemColors.HighlightText;
            this.khoanmuc3.Enabled = false;
            this.khoanmuc3.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.khoanmuc3.Location = new System.Drawing.Point(770, 557);
            this.khoanmuc3.Name = "khoanmuc3";
            this.khoanmuc3.Size = new System.Drawing.Size(10, 21);
            this.khoanmuc3.TabIndex = 5;
            this.khoanmuc3.Visible = false;
            this.khoanmuc3.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
            // 
            // label10
            // 
            this.label10.Enabled = false;
            this.label10.Location = new System.Drawing.Point(774, 557);
            this.label10.Name = "label10";
            this.label10.Size = new System.Drawing.Size(10, 23);
            this.label10.TabIndex = 10;
            this.label10.Text = "K. mục 3 :";
            this.label10.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.label10.Visible = false;
            // 
            // label68
            // 
            this.label68.Location = new System.Drawing.Point(-40, 50);
            this.label68.Name = "label68";
            this.label68.Size = new System.Drawing.Size(88, 23);
            this.label68.TabIndex = 231;
            this.label68.Text = "Nguồn :";
            this.label68.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // c904
            // 
            this.c904.BackColor = System.Drawing.SystemColors.HighlightText;
            this.c904.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.c904.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.c904.Location = new System.Drawing.Point(48, 50);
            this.c904.Name = "c904";
            this.c904.Size = new System.Drawing.Size(168, 21);
            this.c904.TabIndex = 11;
            // 
            // frmImpvienphi
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(792, 542);
            this.Controls.Add(this.label68);
            this.Controls.Add(this.c904);
            this.Controls.Add(this.c700);
            this.Controls.Add(this.khoanmuc2);
            this.Controls.Add(this.khoanmuc4);
            this.Controls.Add(this.label9);
            this.Controls.Add(this.khoanmuc3);
            this.Controls.Add(this.label10);
            this.Controls.Add(this.label8);
            this.Controls.Add(this.khoanmuc);
            this.Controls.Add(this.label7);
            this.Controls.Add(this.c703);
            this.Controls.Add(this.c705);
            this.Controls.Add(this.c710);
            this.Controls.Add(this.label42);
            this.Controls.Add(this.c707);
            this.Controls.Add(this.label39);
            this.Controls.Add(this.c706);
            this.Controls.Add(this.label50);
            this.Controls.Add(this.label38);
            this.Controls.Add(this.c702);
            this.Controls.Add(this.label36);
            this.Controls.Add(this.label37);
            this.Controls.Add(this.label35);
            this.Controls.Add(this.chkAll);
            this.Controls.Add(this.butTonghop);
            this.Controls.Add(this.butKetthuc);
            this.Controls.Add(this.butLuu);
            this.Controls.Add(this.madv);
            this.Controls.Add(this.sophieu);
            this.Controls.Add(this.den);
            this.Controls.Add(this.tu);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.dataGrid1);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.KeyPreview = true;
            this.Name = "frmImpvienphi";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Thu nhập chứng từ viện phí";
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.Load += new System.EventHandler(this.frmImpvienphi_Load);
            this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.frmImpvienphi_MouseMove);
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void frmImpvienphi_Load(object sender, System.EventArgs e)
		{
			try
			{
				user=a.user;iTcnkp=a.iTcnkpv();
				iHoatdong=a.iHoatdongv();iLctug=a.iLctugv();iChuong=a.iChuongv();iNghiepvu=a.iNghiepvuv();iLoaibk=a.iLoaibkv();
				iLk=a.iLkv();iCapphat=a.iCapphatv();sNoidung=a.sNoidungv();iUserid=a.iUseridv();iNhom=a.iNhomv();iKhoanmucno=a.iKhoanmucnov();
			
				foreach(DataRow r in a.get_data("select * from a_bcnghang where id="+iLctug).Tables[0].Rows) tkno=r["tkno"].ToString();
				c700.DisplayMember="TEN";
				c700.ValueMember="ID";
				c700.DataSource=a.get_data("select * from a_hoatdong where sudung=1 order by stt").Tables[0];
				c700.SelectedValue=iHoatdong;

				c702.DisplayMember="TEN";
				c702.ValueMember="ID";
				c702.DataSource=a.get_data("select * from a_dmchuong where sudung=1 order by stt").Tables[0];
				c702.SelectedValue=iChuong;

				c703.DisplayMember="TEN";
				c703.ValueMember="ID";
				c703.DataSource=a.get_data("select * from a_nghiepvu where sudung=1 order by stt").Tables[0];
				c703.SelectedValue=iNghiepvu;	
	
				c904.DisplayMember="TEN";
				c904.ValueMember="ID";
				c904.DataSource=a.get_data("select * from a_dmtcnkp where sudung=1 order by stt").Tables[0];
				c904.SelectedValue=iTcnkp;

				c705.DisplayMember="TEN";
				c705.ValueMember="ID";
				c705.DataSource=a.get_data("select * from a_dmloaibk where sudung=1 order by stt").Tables[0];
				c705.SelectedValue=iLoaibk;
            
				c706.DisplayMember="LK";
				c706.ValueMember="ID";
				c706.DataSource=a.get_data("select id,lk||' '||tlk as lk,tlk from a_dmlk order by stt").Tables[0];
				c706.SelectedValue=iLk;

				c707.DisplayMember="TEN";
				c707.ValueMember="ID";
				c707.DataSource=a.get_data("select * from a_dmcapphat where sudung=1 order by stt").Tables[0];
				c707.SelectedValue=iCapphat;

				c710.DisplayMember="TEN";
				c710.ValueMember="ID";
				c710.DataSource=a.get_data("select * from a_dmnhombc order by stt").Tables[0];
				c710.SelectedValue=iNhom;

				khoanmuc.DisplayMember="TEN";
				khoanmuc.ValueMember="ID";
				khoanmuc.DataSource=a.get_data("select * from a_dmkhoanmuc where sudung=1 order by loaikm,stt").Tables[0];
				khoanmuc.SelectedValue=iKhoanmucno;

				khoanmuc2.DisplayMember="TEN";
				khoanmuc2.ValueMember="ID";
				khoanmuc2.DataSource=a.get_data("select * from a_dmkhoanmuc where sudung=1 order by loaikm,stt").Tables[0];

				khoanmuc3.DisplayMember="TEN";
				khoanmuc3.ValueMember="ID";
				khoanmuc3.DataSource=a.get_data("select * from a_dmkhoanmuc where sudung=1 order by loaikm,stt").Tables[0];

				khoanmuc4.DisplayMember="TEN";
				khoanmuc4.ValueMember="ID";
				khoanmuc4.DataSource=a.get_data("select * from a_dmkhoanmuc where sudung=1 order by loaikm,stt").Tables[0];

				tu.Value=new DateTime(2000+int.Parse(s_mmyy.Substring(2,2)),int.Parse(s_mmyy.Substring(0,2)),1,0,0,0);
				den.Value=new DateTime(int.Parse(s_ngay.Substring(6,4)),int.Parse(s_ngay.Substring(3,2)),int.Parse(s_ngay.Substring(0,2)),0,0,0);			

				load_grid();
				AddGridTableStyle();
			
				this.disabledBackBrush = new SolidBrush(Color.FromArgb(255,255,192));
				this.disabledTextBrush = new SolidBrush(Color.FromArgb(255,0,0));

				this.alertBackBrush = new SolidBrush(SystemColors.HotTrack);
				this.alertFont = new Font(this.dataGrid1.Font.Name, this.dataGrid1.Font.Size, FontStyle.Bold);
				this.alertTextBrush = new SolidBrush(Color.White);
			
				this.currentRowFont = new Font(this.dataGrid1.Font.Name, this.dataGrid1.Font.Size, FontStyle.Regular);
				this.currentRowBackBrush = new SolidBrush(Color.FromArgb(0,255, 255));		

				dttk=a.get_data("select * from a_dmtk order by stk").Tables[0];
				dtloai=a.get_data("select * from "+d.user+".v_loaithutien order by id").Tables[0];
				dtdt=a.get_data("select * from a_dmdt where loaidt=0").Tables[0];
			}
			catch
			{
				MessageBox.Show("Không kết nối được với Viện phí",a.Msg);
			}
		}

		private void load_grid()
		{
			ds=new DataSet();
			ds=a.get_data(f_GetSql());
			ds.Tables[0].Columns.Add("lydo");
			ds.Tables[0].Columns.Add("tkno");
			ds.Tables[0].Columns.Add("tkco");
			ds.Tables[0].Columns.Add("stkno",typeof(decimal));
			ds.Tables[0].Columns.Add("stkco",typeof(decimal));
			ds.Tables[0].Columns.Add("useridkt",typeof(decimal));
			ds.Tables[0].Columns.Add("chon",typeof(bool));
			DataRow r1;
			foreach(DataRow r in ds.Tables[0].Rows)
			{
				r["chon"]=r["ketoan"].ToString()!="0";
				r["lydo"]=(r["loai"].ToString()=="1")?"Thu trực tiếp":(r["loai"].ToString()=="2")?"Thu tạm ứng":"Thu chi ra viện";
				r["tkno"]=tkno;
				r1=a.getrowbyid(dtloai,"id="+int.Parse(r["loai"].ToString()));
				if (r1!=null)
				{
					r["tkco"]=r1["tkco"].ToString();
					r["useridkt"]=r1["userid"].ToString();
				}
				else r["useridkt"]=r["userid"].ToString();
			}
			dataGrid1.DataSource=ds.Tables[0];
			CurrencyManager cm = (CurrencyManager)BindingContext[dataGrid1.DataSource,dataGrid1.DataMember]; 
			DataView dv = (DataView) cm.List; 
			dv.AllowNew = false; 
		}

		private void AddGridTableStyle()
		{
			DataGridTableStyle ts =new DataGridTableStyle();
			ts.MappingName = ds.Tables[0].TableName;
			ts.AlternatingBackColor = Color.Beige;
			ts.BackColor = Color.GhostWhite;
			ts.ForeColor = Color.MidnightBlue;
			ts.GridLineColor = Color.RoyalBlue;
			ts.HeaderBackColor = Color.MidnightBlue;
			ts.HeaderForeColor = Color.Lavender;
			ts.SelectionBackColor = Color.FromArgb(0,255,255);
			ts.SelectionForeColor = Color.PaleGreen;
			ts.RowHeaderWidth=5;
					
			FormattableBooleanColumn discontinuedCol = new FormattableBooleanColumn();
			discontinuedCol.MappingName = "chon";
			discontinuedCol.HeaderText = "Chọn";
			discontinuedCol.Width = 30;
			discontinuedCol.AllowNull = false;

			discontinuedCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
			discontinuedCol.BoolValueChanged += new BoolValueChangedEventHandler(BoolValueChanged);
			ts.GridColumnStyles.Add(discontinuedCol);
			dataGrid1.TableStyles.Add(ts);

			FormattableTextBoxColumn TextCol = new FormattableTextBoxColumn();
			TextCol.MappingName = "mmyy";
			TextCol.HeaderText = "";
			TextCol.Width =0;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol = new FormattableTextBoxColumn();
			TextCol.MappingName = "ketoan";
			TextCol.HeaderText = "";
			TextCol.Width =0;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol = new FormattableTextBoxColumn();
			TextCol.MappingName = "loai";
			TextCol.HeaderText = "";
			TextCol.Width =0;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol = new FormattableTextBoxColumn();
			TextCol.MappingName = "ngay";
			TextCol.HeaderText = "Ngày";
			TextCol.Width =80;
			TextCol.ReadOnly=true;
			TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol = new FormattableTextBoxColumn();
			TextCol.MappingName = "userid";
			TextCol.HeaderText = "";
			TextCol.Width =0;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol = new FormattableTextBoxColumn();
			TextCol.MappingName = "hoten";
			TextCol.HeaderText = "Thu ngân";
			TextCol.Width =200;
			TextCol.ReadOnly=true;
			TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol = new FormattableTextBoxColumn();
			TextCol.MappingName = "lydo";
			TextCol.HeaderText = "Nội dung";
			TextCol.Width =dataGrid1.Width-(20+15+30+80+200+80+80+100);
			TextCol.ReadOnly=true;
			TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new FormattableTextBoxColumn();
			TextCol.MappingName = "tkno";
			TextCol.HeaderText = "Nợ";
			TextCol.Width =80;
			TextCol.ReadOnly=false;
			TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new FormattableTextBoxColumn();
			TextCol.MappingName = "tkco";
			TextCol.HeaderText = "Có";
			TextCol.Width =80;
			TextCol.ReadOnly=false;
			TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new FormattableTextBoxColumn();
			TextCol.MappingName = "sotien";
			TextCol.HeaderText = "Số tiền";
			TextCol.Format="### ### ### ### ###";
			TextCol.Width =100;
			TextCol.Alignment=HorizontalAlignment.Right;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);
		}
	
		private void SetCellFormat(object sender, DataGridFormatCellEventArgs e)
		{
			try
			{
				bool discontinued = (bool) ((e.Column != 0) ? this.dataGrid1[e.Row, 0] : e.CurrentCellValue);
				if(e.Column > 0 && (bool)(this.dataGrid1[e.Row, 0]))//discontinued)
				{
					e.ForeBrush = this.disabledTextBrush;
				}
				else if(e.Column > 0 && e.Row == this.dataGrid1.CurrentRowIndex)
				{
					e.BackBrush = this.currentRowBackBrush;
					e.TextFont = this.currentRowFont;
				}
				if (decimal.Parse(dataGrid1[e.Row,2].ToString())!=0) e.ForeBrush=new SolidBrush(Color.Orange);
			}
			catch{}
		}

		private void BoolValueChanged(object sender, BoolValueChangedEventArgs e)
		{
			this.dataGrid1.EndEdit(this.dataGrid1.TableStyles[0].GridColumnStyles["Discontinued"],e.Row, false);
			RefreshRow(e.Row);
			this.dataGrid1.BeginEdit(this.dataGrid1.TableStyles[0].GridColumnStyles["Discontinued"],e.Row);
		}
		private void RefreshRow(int row)
		{
			Rectangle rect = this.dataGrid1.GetCellBounds(row, 0);
			rect = new Rectangle(rect.Right, rect.Top, this.dataGrid1.Width, rect.Height);
			this.dataGrid1.Invalidate(rect);
		}

		private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
		{
			if((bool)this.dataGrid1[this.dataGrid1.CurrentRowIndex, checkCol])
				this.dataGrid1.CurrentCell = new DataGridCell(this.dataGrid1.CurrentRowIndex, checkCol);
			afterCurrentCellChanged = true;
		}

		private void dataGrid1_Click(object sender, System.EventArgs e)
		{
			Point pt = this.dataGrid1.PointToClient(Control.MousePosition);
			DataGrid.HitTestInfo hti = this.dataGrid1.HitTest(pt);
			BindingManagerBase bmb = this.BindingContext[this.dataGrid1.DataSource, this.dataGrid1.DataMember];
			if(afterCurrentCellChanged && hti.Row < bmb.Count && hti.Type == DataGrid.HitTestType.Cell &&  hti.Column == checkCol )
			{	
				this.dataGrid1[hti.Row, checkCol] = !(bool)this.dataGrid1[hti.Row, checkCol];
				RefreshRow(hti.Row);
			}
			afterCurrentCellChanged = false;
		}

		private void tu_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter) SendKeys.Send("{Tab}");
		}

		private void sophieu_TextChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==sophieu) ref_grid();
		}

		private void madv_TextChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==madv) ref_grid();
		}

		private void ref_grid()
		{
			CurrencyManager cm= (CurrencyManager)BindingContext[dataGrid1.DataSource];
			DataView dv=(DataView)cm.List;
			sql="";
			if (sophieu.Text!="")
			{
				if (sql!="") sql+=" and ";
				sql+=" lydo like '%"+sophieu.Text+"%'";
			}
			if (madv.Text!="")
			{
				if (sql!="") sql+=" and ";
				sql+=" hoten like '%"+madv.Text+"%'";
			}
			dv.RowFilter=sql;
		}

		private string f_GetSql()
		{
			DateTime dt1=a.StringToDate(tu.Text).AddMonths(-1);
			DateTime dt2=a.StringToDate(den.Text).AddMonths(1);
			int y1=dt1.Year,m1=dt1.Month;
			int y2=dt2.Year,m2=dt2.Month;
			int itu,iden;
			string ammyy="",v_mmyy="";sql="";
			bool be=false;
			for(int i=y1;i<=y2;i++)
			{
				itu=(i==y1)?m1:1;
				iden=(i==y2)?m2:12;
				for(int j=itu;j<=iden;j++)
				{
					ammyy=j.ToString().PadLeft(2,'0')+i.ToString().Substring(2,2);
					if (a.bMmyy(ammyy))
					{
						v_mmyy=user+ammyy;
						if(sql!="")  sql+=" union all ";
						//thu truc tiep loai=1
						sql+="select '"+ammyy+"' as mmyy,a.ketoan,1 as loai,to_char(a.ngay,'dd/mm/yyyy') as ngay,a.userid,c.hoten,sum(nvl(b.soluong*b.dongia,0)-nvl(b.mien,0)-nvl(b.thieu,0)) as sotien";
						sql+=" from "+v_mmyy+".v_vienphill a,"+v_mmyy+".v_vienphict b,"+user+".v_dlogin c,";
						sql+="(select * from "+v_mmyy+".v_hoantra ht where to_date(ht.ngay,'dd/mm/yy') between to_date('"+tu.Text+"','dd/mm/yy') and to_date('"+den.Text+"','dd/mm/yy')) x";
						sql+=" where a.id=b.id and a.userid=c.id and a.quyenso=x.quyenso(+) and a.sobienlai=x.sobienlai(+) and x.id is null";
						sql+=" and to_date(a.ngay,'dd/mm/yy') between to_date('"+tu.Text+"','dd/mm/yy') and to_date('"+den.Text+"','dd/mm/yy')";
						sql+=" group by a.ketoan,to_char(a.ngay,'dd/mm/yyyy'),a.userid,c.hoten";
						//thu tamung loai=2
						sql+=" union all ";
						sql+=" select '"+ammyy+"' as mmyy,a.ketoan,2 as loai,to_char(a.ngay,'dd/mm/yyyy') as ngay,a.userid,c.hoten,sum(nvl(a.sotien,0)) as sotien";
						sql+=" from "+v_mmyy+".v_tamung a,"+user+".v_dlogin c,";
						sql+="(select * from "+v_mmyy+".v_hoantra ht where to_date(ht.ngay,'dd/mm/yy') between to_date('"+tu.Text+"','dd/mm/yy') and to_date('"+den.Text+"','dd/mm/yy')) x";
						sql+=" where a.userid=c.id and a.quyenso=x.quyenso(+) and a.sobienlai=x.sobienlai(+) and x.id is null";
						sql+=" and to_date(a.ngay,'dd/mm/yy') between to_date('"+tu.Text+"','dd/mm/yy') and to_date('"+den.Text+"','dd/mm/yy')";
						sql+=" group by a.ketoan,to_char(a.ngay,'dd/mm/yyyy'),a.userid,c.hoten";
						//thu chi ra vien
						sql+=" union all ";
						sql+=" select '"+ammyy+"' as mmyy,a.ketoan,3 as loai,to_char(a.ngay,'dd/mm/yyyy') as ngay,a.userid,c.hoten,sum(nvl(a.sotien,0)-nvl(a.mien,0)-nvl(a.bhyt,0)-nvl(a.tamung,0)) as sotien";
						sql+=" from "+v_mmyy+".v_ttrvll a,"+v_mmyy+".v_ttrvct b,"+user+".v_dlogin c,";
						sql+="(select * from "+v_mmyy+".v_hoantra ht where to_date(ht.ngay,'dd/mm/yy') between to_date('"+tu.Text+"','dd/mm/yy') and to_date('"+den.Text+"','dd/mm/yy')) x";
						sql+=" where a.id=b.id and a.userid=c.id and a.quyenso=x.quyenso(+) and a.sobienlai=x.sobienlai(+) and x.id is null";
						sql+=" and to_date(a.ngay,'dd/mm/yy') between to_date('"+tu.Text+"','dd/mm/yy') and to_date('"+den.Text+"','dd/mm/yy')";
						sql+=" and nvl(a.sotien,0)-nvl(a.mien,0)-nvl(a.bhyt,0)-nvl(a.tamung,0)>0";
						sql+=" group by a.ketoan,to_char(a.ngay,'dd/mm/yyyy'), a.userid, c.hoten";
					}
				}
			}
			return sql;
		}

		private void butTonghop_Click(object sender, System.EventArgs e)
		{
			load_grid();
		}

		private bool kiemtra()
		{
			if (a.bKhoaso(s_mmyy))
			{
				MessageBox.Show("Số liệu tháng "+s_mmyy.Substring(0,2)+" năm 20"+s_mmyy.Substring(2)+" đã khóa sổ !",a.Msg);
				tu.Focus();
				return false;
			}
			string s="",xxx,s1="";
			DataRow r1;
			ds.AcceptChanges();
			foreach(DataRow r in ds.Tables[0].Select("chon=true and ketoan=0"))
			{
				r1=a.getrowbyid(dttk,"stk='"+r["tkno"].ToString()+"'");
				if (r1!=null)	r["stkno"]=r1["id"].ToString();
				else s1+=r["tkno"].ToString()+"\n";

				r1=a.getrowbyid(dttk,"stk='"+r["tkco"].ToString()+"'");
				if (r1!=null)	r["stkco"]=r1["id"].ToString();
				else s1+=r["tkco"].ToString()+"\n";
			}
			ds.AcceptChanges();
			if (s1!="")
			{
				MessageBox.Show("Những tài khoản nợ sau không hợp lệ :\n"+s1,a.Msg);
				tu.Focus();
				return false;
			}
			iHoatdong=int.Parse(c700.SelectedValue.ToString());
			iChuong=int.Parse(c702.SelectedValue.ToString());
			iNghiepvu=int.Parse(c703.SelectedValue.ToString());
			iTcnkp=int.Parse(c904.SelectedValue.ToString());
			iLoaibk=int.Parse(c705.SelectedValue.ToString());
			iLk=int.Parse(c706.SelectedValue.ToString());
			iCapphat=int.Parse(c707.SelectedValue.ToString());
			iNhom=int.Parse(c710.SelectedValue.ToString());
			return true;
		}

		private void butLuu_Click(object sender, System.EventArgs e)
		{
			CurrencyManager cm= (CurrencyManager)BindingContext[dataGrid1.DataSource];
			DataView dv=(DataView)cm.List;
			dv.RowFilter="";
			if (!kiemtra()) return;
			long idct=0,idctct=0,madt=0;
			string xxx,sophieu;
			bool bUpdate=false;
			DataRow r1;
			foreach(DataRow r in ds.Tables[0].Rows)
			{
				xxx=user+r["mmyy"].ToString();
				if (r["chon"].ToString().Trim().ToLower()=="true" && decimal.Parse(r["ketoan"].ToString())==0)
				{
					sophieu=r["ngay"].ToString().Substring(0,2)+r["ngay"].ToString().Substring(3,2)+r["ngay"].ToString().Substring(8,2)+"."+r["loai"].ToString()+"/"+r["userid"].ToString().Trim();
					idct=a.getIdct();madt=0;
					r1=a.getrowbyid(dtdt,"tendt='"+r["hoten"].ToString()+"'");
					if (r1!=null) madt=long.Parse(r1["id"].ToString());
					a.updCt(s_mmyy,idct,iNhom,"",iChuong,iChucnang,iHoatdong,iLctug,sophieu,
						r["ngay"].ToString(),s_ngay,r["lydo"].ToString(),long.Parse(r["stkno"].ToString()),long.Parse(r["stkco"].ToString()),(madt==0)?-1:0,
						madt,Math.Round(decimal.Parse(r["sotien"].ToString()),0),0,s_mmyy.Substring(0,2),iNghiepvu,iLoaibk,iTcnkp,0,"",0,int.Parse(r["useridkt"].ToString()));
					a.updCtk(s_mmyy,idct,r["lydo"].ToString(),"");

					idctct=a.getIdctct();
					a.updCtct(s_mmyy,idctct,idct,1,iLk,r["lydo"].ToString(),long.Parse(r["stkno"].ToString()),long.Parse(r["stkco"].ToString()),
						(khoanmuc.SelectedIndex==-1)?0:long.Parse(khoanmuc.SelectedValue.ToString()),0,
						(khoanmuc2.SelectedIndex==-1)?0:long.Parse(khoanmuc2.SelectedValue.ToString()),0,
						(khoanmuc3.SelectedIndex==-1)?0:long.Parse(khoanmuc3.SelectedValue.ToString()),0,
						(khoanmuc4.SelectedIndex==-1)?0:long.Parse(khoanmuc4.SelectedValue.ToString()),0,0,0,
						Math.Round(decimal.Parse(r["sotien"].ToString()),0),0,0,1,iTonghop,0,iCapphat,iLctug,"",
						r["ngay"].ToString(),iChuong,0,iNghiepvu,0,iHoatdong,int.Parse(r["useridkt"].ToString()));
					if (iTonghop==1)
					{
						a.updSodu("a_sd",a.insert,s_mmyy,iNhom,iChuong,0,iLk,iNghiepvu,0,
							long.Parse(r["stkno"].ToString()),long.Parse(r["stkco"].ToString()),
							(khoanmuc.SelectedIndex==-1)?0:long.Parse(khoanmuc.SelectedValue.ToString()),0,
							(khoanmuc2.SelectedIndex==-1)?0:long.Parse(khoanmuc2.SelectedValue.ToString()),0,
							(khoanmuc3.SelectedIndex==-1)?0:long.Parse(khoanmuc3.SelectedValue.ToString()),0,
							(khoanmuc4.SelectedIndex==-1)?0:long.Parse(khoanmuc4.SelectedValue.ToString()),0,0,0,iHoatdong,
							Math.Round(decimal.Parse(r["sotien"].ToString()),0));
						a.updSodu("a_sdc",a.insert,s_mmyy,iNhom,iChuong,0,iLk,iNghiepvu,0,long.Parse(r["stkno"].ToString()),long.Parse(r["stkco"].ToString()),
							(khoanmuc.SelectedIndex==-1)?0:long.Parse(khoanmuc.SelectedValue.ToString()),0,
							(khoanmuc2.SelectedIndex==-1)?0:long.Parse(khoanmuc2.SelectedValue.ToString()),0,
							(khoanmuc3.SelectedIndex==-1)?0:long.Parse(khoanmuc3.SelectedValue.ToString()),0,
							(khoanmuc4.SelectedIndex==-1)?0:long.Parse(khoanmuc4.SelectedValue.ToString()),0,0,0,iHoatdong,
							Math.Round(decimal.Parse(r["sotien"].ToString()),0));
						if (a.bChophanbo(long.Parse(r["stkno"].ToString()),long.Parse(r["stkco"].ToString())))
							a.updPhanbo("a_phanbochua",a.insert,idct,idctct,iLctug,sophieu,r["ngay"].ToString(),
							long.Parse(r["stkno"].ToString()),long.Parse(r["stkco"].ToString()),
							"",r["lydo"].ToString(),1,Math.Round(decimal.Parse(r["sotien"].ToString()),0),iNghiepvu,s_mmyy,int.Parse(r["useridkt"].ToString()));
					}					
					if (r["loai"].ToString()=="1") sql="update "+xxx+".v_vienphill set ketoan="+idct+" where to_char(ngay,'dd/mm/yyyy')='"+r["ngay"].ToString()+"' and userid="+long.Parse(r["userid"].ToString())+" and ketoan=0";
					else if (r["loai"].ToString()=="2") sql="update "+xxx+".v_tamung set ketoan="+idct+" where to_char(ngay,'dd/mm/yyyy')='"+r["ngay"].ToString()+"' and userid="+long.Parse(r["userid"].ToString())+" and ketoan=0";
					else sql="update "+xxx+".v_ttrvll set ketoan="+idct+" where to_char(ngay,'dd/mm/yyyy')='"+r["ngay"].ToString()+"' and userid="+long.Parse(r["userid"].ToString())+" and ketoan=0";
					a.execute_data(sql);
					a.execute_data("update "+xxx+".v_hoantra set ketoan="+idct+" where to_char(ngay,'dd/mm/yyyy')='"+r["ngay"].ToString()+"' and userid="+long.Parse(r["userid"].ToString())+" and ketoan=0");
				}
				else if (r["chon"].ToString().Trim().ToLower()=="false" && decimal.Parse(r["ketoan"].ToString())!=0)
				{
					a.execute_data("delete from "+user+".a_ctct"+s_mmyy.Substring(2)+" where id="+long.Parse(r["ketoan"].ToString()));
					a.execute_data("delete from "+user+".a_ct"+s_mmyy.Substring(2)+" where id="+long.Parse(r["ketoan"].ToString()));
					a.execute_data("delete from "+user+".a_ctk"+s_mmyy.Substring(2)+" where id="+long.Parse(r["ketoan"].ToString()));					
					if (r["loai"].ToString()=="1") sql="update "+xxx+".v_vienphill set ketoan=0 where to_char(ngay,'dd/mm/yyyy')='"+r["ngay"].ToString()+"' and userid="+long.Parse(r["userid"].ToString())+" and ketoan="+long.Parse(r["ketoan"].ToString());
					else if (r["loai"].ToString()=="2") sql="update "+xxx+".v_tamung set ketoan=0 where to_char(ngay,'dd/mm/yyyy')='"+r["ngay"].ToString()+"' and userid="+long.Parse(r["userid"].ToString())+" and ketoan="+long.Parse(r["ketoan"].ToString());
					else sql="update "+xxx+".v_ttrvll set ketoan=0 where to_char(ngay,'dd/mm/yyyy')='"+r["ngay"].ToString()+"' and userid="+long.Parse(r["userid"].ToString())+" and ketoan="+long.Parse(r["ketoan"].ToString());
					a.execute_data(sql);
					a.execute_data("update "+xxx+".v_hoantra set ketoan=0 where to_char(ngay,'dd/mm/yyyy')='"+r["ngay"].ToString()+"' and userid="+long.Parse(r["userid"].ToString())+" and ketoan="+long.Parse(r["ketoan"].ToString()));
				}
				bUpdate=true;
			}
			if (bUpdate) a.updDtsd("",s_mmyy,iUserid);
			load_grid();
			butTonghop.Focus();
		}

		private void butKetthuc_Click(object sender, System.EventArgs e)
		{
			a.close();System.GC.Collect();this.Close();
		}

		private void chkAll_CheckedChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==chkAll)
				foreach(DataRow r in ds.Tables[0].Select("ketoan=0")) r["chon"]=(chkAll.Checked)?true:false;
		}

		private void frmImpvienphi_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if (bClear)
			{
				khoanmuc2.SelectedIndex=khoanmuc3.SelectedIndex=khoanmuc4.SelectedIndex=-1;
				bClear=false;
			}
		}
	}
}		
